<template>
  <div class="app-container">
    <h4>司机管理</h4>
    <el-form ref="form" class="el_one" :model="queryParams">
      <el-row>
        <el-col :span="8">
          <div class="grid-content">
            <el-form-item label="司机账号:" prop="account">
              <el-input v-model="queryParams.account" placeholder="请输入司机账号" style="width: 250px;" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content">
            <el-form-item label="司机姓名:" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入司机姓名" style="width: 250px;" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content">
            <el-form-item label="司机手机号:" prop="phone">
              <el-input v-model="queryParams.phone" placeholder="请输入司机手机号" style="width: 250px;" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="institution-content">
            <el-form-item label="所属机构:" prop="region">
              <el-select ref="select" v-model="region" placeholder="请选择所属机构" style="width: 250px;">
                <el-option
                  value=""
                  style="display:none"
                />
                <el-tree
                  :data="dataTerr"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  @node-click="handleNodeClick"
                />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="buttod-content" style="padding-left: 30px; padding-right: 30px;">
            <el-form-item>
              <el-button type="danger" style="width: 90px" @click="fnSearch">搜索</el-button>
              <el-button style="width: 90px" @click="fnReset">重置</el-button>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div class="el-card__body">
      <el-table stripe :data="list" style="width: 100%">
        <el-table-column type="index" border label="序号" width="100" />
        <el-table-column prop="account" label="司机账号" width="200" />
        <el-table-column prop="name" label="司机姓名" width="200" />
        <el-table-column prop="mobile" label="司机电话" width="250" />
        <el-table-column prop="truck.licensePlate" label="关联车辆" width="300" />
        <el-table-column prop="agency.name" label="所属机构" width="230" />
        <el-table-column :formatter="fnFormatter" prop="workStatus" label="工作状态" width="230" />
        <el-table-column fixed="right" label="操作" width="130px">
          <template>
            <el-button type="text" size="small" class="editbutton" @click="fnExamine">查看</el-button>
          </template>
          <span class="line"> | </span>
          <template>
            <el-button type="text" size="small" class="editbutton" @click="open">配置车辆</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="queryParams.page"
          :page-sizes="[10, 20, 30, 100]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="+counts"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getDriver, getTree } from '@/api/staff'
export default {
  data() {
    return {
      dataTerr: [],
      defaultProps: {
        children: 'children', // 控制树形的子集
        label: 'name' // 控制树形节点显示的文字
      },
      queryParams: {
        pageSize: 10,
        page: 1,
        account: '',
        agentId: '',
        name: '',
        phone: ''
      },
      region: '',
      list: [],
      counts: 0
    }
  },
  created() {
    this.driverData()
    this.getTreeData()
  },
  methods: {
    // 点击跳转
    fnExamine() {
      this.$router.push('/user/examine')
    },
    open() {
      this.$confirm('<div>配置车辆需要满足以下条件:</div><span>1 司机信息已完善</span><span style="margin-left:100px">2 司机已设置排班</span><div>3.司机绑定车辆未在启用状态</div>', '车辆配置', {
        dangerouslyUseHTMLString: true
      })
    },
    async getTreeData() {
      const result = await getTree()
      console.log(JSON.parse(result))
      this.dataTerr = JSON.parse(result)
    },
    async driverData() {
      const result = await getDriver({
        pageSize: this.queryParams.pageSize,
        page: this.queryParams.page
      })
      console.log(result)
      this.list = result.items
      this.counts = result.counts
    },
    // 序号
    indexMethod(index) {
      return index++
    },
    // 每页条数切换得处理函数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryParams.pageSize = val
      this.driverData()
    },
    // 当前页码处理的函数
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)// val:切换的当前页码
      // 修改当前页码,重新发起请求
      this.queryParams.page = val
      this.driverData()
    },
    // 搜索
    async fnSearch() {
      const res = await getDriver(this.queryParams)
      console.log(res)
      this.list = res.items
      this.counts = res.counts
    },
    // 重置
    fnReset() {
      this.$refs.form.resetFields()
      this.region = ''
      this.queryParams.page = 1
      this.driverData()
    },
    // 树形点击
    handleNodeClick(data) {
      console.log(data)
      this.region = data.name
      this.queryParams.agentId = data.id
      this.queryParams.page = 1
      this.$refs.select.blur()
    },
    // 工作状态
    fnFormatter(row, column, cellValue) {
      return cellValue === 1 ? '上班' : cellValue === 0 ? '休息' : '无'
    }
  }
}
</script>

<style lang="scss">
.editbutton{
  color: #419eff;
}
.el_one{
  width: 100%;
  height: 150px;
  background-color: #fff;
}
.grid-content{
  margin-left: 40px;
  margin-top: 20px;
}
.institution-content{
  margin-left: 40px;
}
.el-card__body{
  width: 100%;
  background-color: #fff;
  margin-top: 20px;
  padding:28px 28px 28px 28px;
}
.block{
  margin-left: 330px;
  margin-top: 20px;
}
.line {
  color:#dcdfe6;
  margin-right: 5px;
  margin-left: 5px;
}
</style>
